package scenic;

import java.awt.Rectangle;

import scenic.geom.Affine2;
import scenic.path.Path;
import scenic.path.PathWalker;

/**
 * FilledPath class fills the area inside the given path.
 */
public class FilledPath extends Shape {
	private Path path;
	private int fillRule;

	/**
	 * Constructs a filled path using the given path.
	 * 
	 * @param path the path to be filled.
	 */
	public FilledPath(Path path) {
		this.path = path;
	}
	
	private class Walker implements PathWalker
	{
		private DrawContext context;
		
		public Walker(DrawContext context) {
			this.context = context;
		}
		
		public void beginSubPath(boolean isClosed) {
			context.renderer.tessBeginContour(context.context);			
		}

		public void endSubPath() {
			context.renderer.tessEndContour(context.context);			
		}
	
		public void lineTo(double x, double y) {
			context.renderer.tessVertex(context.context, x, y);			
		}
	}
	
	void draw(DrawContext context, Affine2 transform, Color color) {
		context.renderer.setTransform(context.context, transform);
		context.renderer.color(context.context, color);
		context.renderer.setPolygonAntialias(context.context, isAntialias());
		context.renderer.tessBegin(context.context);
		path.walk(new Walker(context), Affine2.multiply(transform, 8.0));
		context.renderer.tessEnd(context.context);
	}
	
	/**
	 * Sets the path to be filled.
	 * 
	 * @param path the path to filled.
	 */
	public void setPath(Path path) {
		this.path = path;
		changed();
	}

	/**
	 * Gets the path to be filled.
	 * 
	 * @return the path to be filled.
	 */
	public Path getPath() {
		return path;
	}
	
	/**
	 * Sets the fill rule used when filling the path. The fill
	 * rule must be one of the constants defined in the FillRule
	 * interface.
	 * 
	 * @param rule the fill rule. 
	 */
	public void setFillRule(int rule) {
		fillRule = rule;
		changed();
	}

	/**
	 * Gets the fill rule.
	 */
	public int getFillRule() {
		return fillRule;
	}
	
	int getDrawType(Affine2 transform, Color color) {
		return isAntialias() ? DRAW_SURFACE4X : DRAW_SIMPLE;
	}
	
	Rectangle getBounds(Affine2 transform) {
		return path.getBounds(transform);
	}	
	
}
